/**
 Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ta.web;


import java.io.IOException;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.ta.entity.StUserSign;
import com.thinkgem.jeesite.modules.ta.service.StUserSignService;

/**
 * 用户签名管理Controller
 * 
 * @author TTAO
 * @version 2017-05-31
 */
@Controller
@RequestMapping(value = "${adminPath}/ta/stUserSign")
public class StUserSignController extends BaseController {

	@Autowired
	private StUserSignService stUserSignService;

	@ModelAttribute
	public StUserSign get(@RequestParam(required = false) String id) {
		StUserSign entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = stUserSignService.get(id);
		}
		if (entity == null) {
			entity = new StUserSign();
		}
		return entity;
	}

	@RequiresPermissions("ta:stUserSign:view")
	@RequestMapping(value = { "index" })
	public String index(User user, Model model) {
		return "modules/ta/stDepartmentTree";
	}

	@RequiresPermissions("ta:stUserSign:view")
	@RequestMapping(value = { "list", "" })
	public String list(String departid, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		// Page<StUserSign> page = stUserSignService.findPage(
		// new Page<StUserSign>(request, response), stUserSign);
		List<StUserSign> page = stUserSignService.getListByDepartId(departid);
		model.addAttribute("page", page);
		return "modules/ta/stUserSignList";
	}
	
	@RequiresPermissions("ta:stUserSign:view")
	@RequestMapping(value = { "findQName", "" })
	public String findQName(String userid, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		// Page<StUserSign> page = stUserSignService.findPage(
		// new Page<StUserSign>(request, response), stUserSign);
		List<String> ls = stUserSignService.findQName(userid);
		model.addAttribute("ls", ls);
		return "modules/ta/QNameList";
	}
	

	@RequiresPermissions("ta:stUserSign:view")
	@RequestMapping(value = "form")
	public String form(StUserSign stUserSign, Model model) {
		model.addAttribute("stUserSign", stUserSign);
		return "modules/ta/stUserSignForm";
	}

	@RequiresPermissions("ta:stUserSign:edit")
	@RequestMapping(value = "save")
	public String save(@RequestParam("file") MultipartFile file,String userid, Model model,
			RedirectAttributes redirectAttributes) throws IOException {
		if(file == null){
			return "redirect:" + Global.getAdminPath() + "/ta/stUserSign/index";
		}
		StUserSign stUserSign = new StUserSign();
		stUserSign.setUserid(userid);
		stUserSign.setSavedate(new Date());
		byte[] bytes = file.getBytes();
		stUserSign.setSignpic(bytes);
		stUserSignService.updateSign(stUserSign);
		addMessage(redirectAttributes, "修改用户签名成功");
		return "redirect:" + Global.getAdminPath() + "/ta/stUserSign/index";
	}

	@RequiresPermissions("ta:stUserSign:edit")
	@RequestMapping(value = "delete")
	public String delete(StUserSign stUserSign,
			RedirectAttributes redirectAttributes) {
		stUserSignService.delete(stUserSign);
		addMessage(redirectAttributes, "删除用户签名成功");
		return "redirect:" + Global.getAdminPath() + "/ta/stUserSign/?repage";
	}

}